import axios from "axios";
import { getToken } from "./token";
// 创建axios实例
const instance = axios.create({
  // axios中请求配置有baseURL选项，表示请求URL公共部分
  //   baseURL: "http://localhost:3001/api",
  headers: {
    "Content-Type": "application/json",
  },
  withCredentials: true, // 允许跨域请求时携带cookie
  // 超时
  timeout: 10000,
});
instance.interceptors.request.use(
  (config) => {
    const token = getToken();
    if (token) {
      config.headers["Authorization"] = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
instance.interceptors.response.use(
  (res) => {
    console.log(res);
    if (res.data.code === 200) {
      return res.data;
    }
  },
  (error) => {
    return Promise.reject;
  }
);
export default instance;
